package ComparableTest;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class User implements Comparable<User> {
    private String name;
    private int age;
    private int gender;
    private String address;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * @return the gender
     */
    public int getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(int gender) {
        this.gender = gender;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     *
     */
    public User() {
        super();
    }

    /**
     * @param name
     * @param age
     * @param gender
     * @param address
     */
    public User(String name, int age, int gender, String address) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.address = address;
    }

    @Override
    public int compareTo(User o) {
        if (o != null) {
            if (this.getAge() > o.getAge()) {
                return 1;
            } else if (this.getAge() == o.getAge()) {
                return 0;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        List<User> ulist = new ArrayList<User>();
        ulist.add(new User("wangbo", 29, 1, "长沙"));
        ulist.add(new User("wumei", 44, 1, "株洲"));
        ulist.add(new User("zhangjie", 19, 1, "岳阳"));
        ulist.add(new User("lihua", 36, 1, "长沙"));
        ulist.add(new User("zhangchangzhe", 19, 1, "衡阳"));
        Collections.sort(ulist);
        for (User u : ulist) {
            System.out.println(u.getName() + "\t" + u.getAge() + "\t" + u.getAddress() + "\t" + u.getGender());
        }
    }
}
